package com.example.fifthproject;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static int MODE = MODE_PRIVATE;
    public static final String PREFERENCE_NAME = "SaveSetting";
    Button btnLogin;
    EditText etAccount, etPassword;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);
/**完成代码***/
        sharedPreferences=getSharedPreferences(PREFERENCE_NAME, MODE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username",etAccount.getText().toString());
        editor.putString("password",etPassword.getText().toString());
        editor.commit();
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
/**完成代码***/
                String account=sharedPreferences.getString("username","");
                String password=sharedPreferences.getString("password","");
                if(!account.equals("")){
                    etAccount.setText(account);
                    etPassword.setText(password);
                }
                Toast.makeText(this,"您已成功登录",Toast.LENGTH_SHORT).show();
                break;
        }
    }

}